<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\common\lib\Data;
use think\facade\Cache;
class Category extends Common {
	
	protected $data;
	
	public function __construct(){
	    parent::__construct();
	
	    $this->data = new Data();
	}
	
	public function index(){
		return $this->fetch();
	}
	public function catetree() {
		$cate = db('category')
				->alias('a')
				->leftJoin('models b','a.model_id = b.id')
				->order('a.sort asc')
				->cache('category',3600,'category')
				->column('a.id,a.pid,a.cat_pic,a.name,a.sort,a.model_id,a.status,b.name as model_name');
		// p($cate );die;
		foreach($cate as $k=>$v){
			if(empty($v['model_name'])){
				$cate[$k]['model_name']="自定义模型";
			}
			if($this->data->hasChild($cate,$v['id'])){
				$cate[$k]['isParent'] = true;
			}
		}
		
		// p($cate );die;
		$result = $this->data->tree($cate,'name','id');
	    return showTable(0, 'OK',100, $result, 200);
	}
	
	
	//改变现实状态
	public function status(){
		$data = request()->param();
		$ndata = ['status'=>$data['status']];
		$result = db('category')->where('id', $data['id'])->update($ndata);
		Cache::clear('category');
		return show(1, 'OK', $result, 200);
	}
	
	//改变排序
	public function changeSort(){
		$data = request()->param();
		$ndata = ['sort'=>$data['sort']];
		
		$result = db('category')->where('id', $data['id'])->update($ndata);
		Cache::clear('category');
		return show(1, 'OK', $result, 200);
	}
	
	public function add(){
		$id = request()->param('id')?request()->param('id'):0;
		$model_id = request()->param('model_id');
		$cate = db('category')->select();
		// p($cate);die;
		$category = $this->data->tree($cate, 'name', 'id');
		// p($category);die;
		$modelList     = db('models')->where(['status' =>1])->order('sort')->select();
		$category_temp = get_file_folder_List('template/index',2,'category_*');
		$list_temp = get_file_folder_List('template/index',2,'list_*');
		$show_temp = get_file_folder_List('template/index',2,'show_*');
		$this->assign('model_id',$model_id);
		$this->assign('mlist',$modelList);
		$this->assign('temp_category',$category_temp);
		$this->assign('temp_list',$list_temp);
		$this->assign('temp_show',$show_temp);
		$this->assign('cate',$category);
		$this->assign('id',$id);
		return $this->fetch();
	}
	public function custom(){
		$id = request()->param('id')?request()->param('id'):0;
		$model_id = request()->param('model_id');
		$cate = db('category')->select();
		// p($cate);die;
		$category = $this->data->tree($cate, 'name', 'id');
		// p($category);die;
		$modelList     = db('models')->where(['status' =>1])->order('sort')->select();
		$show = get_file_folder_List('template/index',2,'custom_*');
		$this->assign('model_id',$model_id);
		$this->assign('temp_show',$show);
		
		$this->assign('cate',$category);
		$this->assign('id',$id);
		return $this->fetch();
	}
	
	public function add_form(){
		$data = request()->param();
		// p($data);die;
		if($data['type'] == 5){
			if(strlen($data['url'])<10){
				return show(1, "外链不正确", "", 200);
			}
		}
		if (empty($data['ename'])) {
			$data['ename'] = get_pinyin(iconv('utf-8', 'GBK//ignore', $data['name']), 0, 1);
		}else{
			$data['ename'] = $data['ename'];
		}
		$rest = db('category')->insert($data);
		Cache::clear('category');
		if($rest){
			return show(0, 'OK', $rest, 200);
		}else{
			return show(1, 'error', $rest, 200);
		}

//        $str = "1-王夏雨1111";
//        $preg = "/[\x{4e00}-\x{9fa5}]+/u";
//        if(preg_match_all($preg,$str,$matches)){
//            $py = substr(strtoupper(get_pinyin(iconv('utf-8', 'GBK//ignore', $matches[0][0]), 0, 1)),0,1);
//            p($py);die;
//        }
		
	}
	
	public function cate_delete(){
		$data = request()->param();
		// p($data);die;
		if(isset($data['isParent'])){
			return show(1, "该栏目地下存在子栏目，请先删除子栏目", [], 200);
		}else{
			$result = Db::name('category')->delete(['id'=>$data['id']]);
			Cache::clear('category');
			return show(0, 'OK', [], 200);
		}
	}
	
	public function edit(){
		$id = request()->param('id')?request()->param('id'):0;
		$model_id = request()->param('model_id');
		$curarr = db('category')->where(['id'=>$id])->find();
		$cate = db('category')->select();
		// p($cate);die;
		$category = $this->data->tree($cate, 'name', 'id');
		// p($category);die;
		$modelList     = db('models')->where(['status' =>1])->order('sort')->select();
		$list = get_file_folder_List('template/index',2,'list_*');
		$show = get_file_folder_List('template/index',2,'show_*');
		$this->assign('model_id',$model_id);
		$this->assign('mlist',$modelList);
		$this->assign('temp_list',$list);
		$this->assign('temp_show',$show);
		$this->assign('cate',$category);
		$this->assign('id',$id);
		$this->assign('curarr',$curarr);
		return $this->fetch();
	}
	
	public function edit_add(){
		$data = request()->param();
		$d['template_list'] = $data['template_list'];
		$d['template_show'] = $data['template_show'];
		if (empty($data['ename'])) {
			$d['ename'] = get_pinyin(iconv('utf-8', 'GBK//ignore', $data['name']), 0, 1);
		}else{
			$d['ename'] = $data['ename'];
		}
		$d['pid'] = $data['pid'];
		$d['model_id'] = $data['model_id'];
		$d['name'] = $data['name'];
		$d['status'] =isset($data['status'])?$data['status']:0;
		$d['description'] = $data['description'];
		
		$rest = db('category')->where(['id'=>$data['id']])->update($d);
		if($rest){
			return show(0, 'OK', $rest, 200);
		}else{
			return show(1, 'error', $rest, 200);
		}
		
	}
	
	public function get_temp(){
		
		$model_id = request()->param('model_id');
		$rest     = db('models')->where(['id' =>$model_id])->find();
		return show(0, 'OK', $rest, 200);
	}
	
	
}